package com.wcs.spring.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wcs.spring.entity.ClassTable;
import com.wcs.spring.entity.OrderClass;
import com.wcs.spring.entity.User;
import com.wcs.spring.service.ClassTableService;
import com.wcs.spring.util.Const;
import com.wcs.spring.util.WeekDateUtil;

@Controller
@RequestMapping(value = "ph/classTable")
public class PhClassTableController {
	@Resource(name = "classTableService")
	private ClassTableService classTableService;

	@RequestMapping(value = "myOrderClass")
	public @ResponseBody
	Map<String, Object> myOrderClass(HttpServletRequest request) throws Exception {
		User user = (User) request.getSession().getAttribute(Const.SESSION_PH_USER);
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> weekDateList = WeekDateUtil.getWeekdays(null);
		String beginDate = weekDateList.get(0);
		String endDate = weekDateList.get(6);
		OrderClass orderClass = new OrderClass();
		orderClass.setSchoolId(user.getSchoolId());
		orderClass.setStudentId(user.getId());
		List<ClassTable> classTableList = this.classTableService.queryOrderClassListPh(orderClass, beginDate, endDate);

		map.put("classTableList", classTableList);

		return map;
	}

	@RequestMapping(value = "nowDayClass")
	public ModelAndView nowDayClass(ClassTable classTable, HttpServletRequest request) throws Exception {
		// User user = (User) request.getSession().getAttribute(Const.SESSION_PH_USER);
		Map<String, Object> map = new HashMap<String, Object>();
		// String now = WeekDateUtil.dateToString(new Date(), "yyyy-MM-dd");
		List<String> weekDateList = WeekDateUtil.getWeekdays(null);
		// classTable.setClassDate(new Date());
		// List<ClassTable> classTableList = this.classTableService.queryClassTableListPh(classTable, user.getId());
		//
		// map.put("classTableList", classTableList);
		map.put("weekDateList", weekDateList);
		String[] weekStr = { "Mon", "Tue", "The", "Wed", "Fri", "Sat", "Sun" };
		map.put("weekStr", weekStr);
		return new ModelAndView("ph/order", map);
	}

	@RequestMapping(value = "queryClassTableByDay")
	public @ResponseBody
	Map<String, Object> queryClassTableByDay(ClassTable classTable, HttpServletRequest request) throws Exception {
		User user = (User) request.getSession().getAttribute(Const.SESSION_PH_USER);
		Map<String, Object> map = new HashMap<String, Object>();
		if (classTable.getClassDate() == null) {
			classTable.setClassDate(new Date());
		}
		classTable.setSchoolId(user.getSchoolId());
		List<ClassTable> classTableList = this.classTableService.queryClassTableListPh(classTable, user.getId());

		map.put("classTableList", classTableList);
		return map;
	}

	@RequestMapping(value = "orderClass")
	public @ResponseBody
	Map<String, Object> orderClass(Integer id, HttpServletRequest request) throws Exception {
		User user = (User) request.getSession().getAttribute(Const.SESSION_PH_USER);
		Map<String, Object> map = new HashMap<String, Object>();
		String message = "";
		ClassTable classTable = classTableService.queryClassTableById(id);
		if (classTable.getClassLevel().contains("," + user.getStudentLevel() + ",")) {
			if (classTable.getOrderClassNum() <= classTable.getStudentNum()) {
				OrderClass orderClass = new OrderClass();
				orderClass.setStudentId(user.getId());
				orderClass.setSchoolId(user.getSchoolId());
				orderClass.setClassTableId(id);
				orderClass.setCreateTime(new Date());
				orderClass.setUpdateTime(new Date());
				orderClass.setStatus(0);
				classTableService.addOrderClass(orderClass);
				// String now = WeekDateUtil.dateToString(new Date(), "yyyy-MM-dd");

				map.put("flag", 0);
			}
			else {
				message = "对不起，此课程人数已满！";
				map.put("flag", 1);
				map.put("message", message);
			}
		}
		else {
			message = "对不起，您不能预订此级别的课程！";
			map.put("flag", 1);
			map.put("message", message);
		}

		return map;
	}

	@RequestMapping(value = "cancelClass")
	public @ResponseBody
	Map<String, Object> cancelClass(Integer id, HttpServletRequest request) throws Exception {
		User user = (User) request.getSession().getAttribute(Const.SESSION_PH_USER);
		Map<String, Object> map = new HashMap<String, Object>();
		OrderClass orderClass = new OrderClass();
		orderClass.setClassTableId(id);
		orderClass.setUpdateTime(new Date());
		orderClass.setStatus(1);
		orderClass.setStudentId(user.getId());
		orderClass.setClassTableId(id);
		classTableService.updateOrderClass(orderClass);
		// String now = WeekDateUtil.dateToString(new Date(), "yyyy-MM-dd");

		map.put("flag", 0);
		return map;
	}

}
